﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using DLLPC.Utilities;



namespace COER
{
    public partial class UC_MainSub : UserControl
    {
        public UC_MainSub()
        {
            InitializeComponent();
        }

        public void Manual_Load()
        {
            GetControls(this.Controls);
            Init();
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tabControl2.Enabled =  true;
            //cbxScanLoop.Enabled = Kernel.OP.Level > 0 && ProcedureHelper_EQ.ManualMode && ProcedureHelper_EQ.DryRunMode;
            //btnULDSTULDTray.EEnabled = ProcedureHelper_EQ.AutoMode;
            //btnULDSTULDTray.Visible = ProcedureHelper_EQ.AutoMode ? true:false;
            //btnSortSTULDTray.EEnabled = ProcedureHelper_EQ.AutoMode;
            //btnSortSTULDTray.Visible = ProcedureHelper_EQ.AutoMode ? true : false;

            //ProcedureHelper_Efem.ALGPass_Frame = true;//Efem ALG强制Pass
            //cbxEfemALGPass.Checked = true;

            ProcedureHelper_Exc.SortSTFrameCheckPass = true;//SortST FrameCheck强制Pass
                                                            //cbxSortFrameCheckPass.Checked = true; 

            //if (cbx_signalTest.Checked)
            //{
            //    btn_ToUp_CH1_Ready.Enabled = true;
            //    btn_ToUp_CH2_Ready.Enabled = true;
            //    btn_ToUp_CH3_Ready.Enabled = true;
            //    btn_ToUp_CH4_Ready.Enabled = true;
            //    btn_ToUp_CH1_Receiving.Enabled = true;
            //    btn_ToUp_CH2_Receiving.Enabled = true;
            //    btn_ToUp_CH3_Receiving.Enabled = true;
            //    btn_ToUp_CH4_Receiving.Enabled = true;

            //    btn_ToDn_CH1_Rdy.Enabled = true;
            //    btn_ToDn_CH2_Rdy.Enabled = true;
            //    btn_ToDn_CH3_Rdy.Enabled = true;
            //    btn_ToDn_CH4_Rdy.Enabled = true;
            //    btn_ToDn_CH1_Sending.Enabled = true;
            //    btn_ToDn_CH2_Sending.Enabled = true;
            //    btn_ToDn_CH3_Sending.Enabled = true;
            //    btn_ToDn_CH4_Sending.Enabled = true;

            //}
            //else
            //{
            //    btn_ToUp_CH1_Ready.Enabled = false;
            //    btn_ToUp_CH2_Ready.Enabled = false;
            //    btn_ToUp_CH3_Ready.Enabled = false;
            //    btn_ToUp_CH4_Ready.Enabled = false;
            //    btn_ToUp_CH1_Receiving.Enabled = false;
            //    btn_ToUp_CH2_Receiving.Enabled = false;
            //    btn_ToUp_CH3_Receiving.Enabled = false;
            //    btn_ToUp_CH4_Receiving.Enabled = false;

            //    btn_ToDn_CH1_Rdy.Enabled = false;
            //    btn_ToDn_CH2_Rdy.Enabled = false;
            //    btn_ToDn_CH3_Rdy.Enabled = false;
            //    btn_ToDn_CH4_Rdy.Enabled = false;
            //    btn_ToDn_CH1_Sending.Enabled = false;
            //    btn_ToDn_CH2_Sending.Enabled = false;
            //    btn_ToDn_CH3_Sending.Enabled = false;
            //    btn_ToDn_CH4_Sending.Enabled = false;

            //}
          

            cbxInsp1Pass.Enabled = ProcedureHelper_EQ.ManualMode;
            cbxInsp2Pass.Enabled = ProcedureHelper_EQ.ManualMode;
            cbxInsp3Pass.Enabled = ProcedureHelper_EQ.ManualMode;
            cbxInsp4Pass.Enabled = ProcedureHelper_EQ.ManualMode;
           
            label2.Enabled = Kernel.OP.Level > 0 && ProcedureHelper_EQ.ManualMode;          
            FreshStatus();
        }

        private void Init()
        {
            cbx_signalTest.Checked = false;           
        }
        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }


                if (item is CheckBox && item.Tag?.ToString().Split('|')[0] == "cbx")
                {
                    ((CheckBox)item).CheckedChanged += new EventHandler(this.cbx_CheckedChanged);
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        IOView frmIOView;
        RTCView frmRTCView;
        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "IOShow":
                    {
                        if (frmIOView == null)
                        {
                            frmIOView = new IOView();
                        }
                        frmIOView.ShowForm(Kernel.motion);
                    }
                    break;
                case "RTCShow":
                    {
                        if (frmRTCView == null)
                        {
                            frmRTCView = new RTCView();
                        }
                        frmRTCView.ShowForm();
                    }
                    break;
                case "ToUp_CH1_Ready":
                    {

                    }
                    break;
                case "ToUp_CH2_Ready":
                    {

                    }
                    break;
                case "ToUp_CH3_Ready":
                    {

                    }
                    break;
                case "ToUp_CH4_Ready":
                    {

                    }
                    break;
                case "ToUp_CH1_Receiving":
                    {
                                  
                    }
                    break;
                case "ToUp_CH2_Receiving":
                    {
                      
                    }
                    break;               
                case "ToUp_CH3_Receiving":
                    {
                       
                    }
                    break;
                case "ToUp_CH4_Receiving":
                    {
                       
                    }
                    break;
                case "ToDn_CH1_Rdy":
                    {

                    }
                    break;
                case "ToDn_CH2_Rdy":
                    {

                    }
                    break;
                case "ToDn_CH3_Rdy":
                    {

                    }
                    break;
                case "ToDn_CH4_Rdy":
                    {

                    }
                    break;
                case "ToDn_CH1_Sending":
                    {

                    }
                    break;
                case "ToDn_CH2_Sending":
                    {

                    }
                    break;
                case "ToDn_CH3_Sending":
                    {

                    }
                    break;
                case "ToDn_CH4_Sending":
                    {

                    }
                    break;
                case "Train":
                    {
                    }
                    break;
                case "ExchangeStart":
                    {
                        //ProcedureHelper_EQ.ExchangeStart = !ProcedureHelper_EQ.ExchangeStart;
                    }
                    break;
                case "RepeatTry":
                    {
                       
                    }
                    break;
                case "":
                    {

                    }
                    break;
              
                case "ReInsp_Insp":
                    {

                    }
                    break;
                case "Insp1_Next":                    
                    {
                        ProcedureHelper_Exc.Insp1_ToNext = true;
                    }
                    break;
                case "Insp2_Next":
                    {
                        ProcedureHelper_Exc.Insp2_ToNext = true;
                    }
                    break;
                case "Insp3_Next":
                    {
                        ProcedureHelper_Exc.Insp3_ToNext = true;
                    }
                    break;
                case "Insp4_Next":
                    {
                        ProcedureHelper_Exc.Insp4_ToNext = true;
                    }
                    break;
                case "Insp1TimesClear":
                    {
                        ParameterHelper.SysParam.LifeTime.Clamp.Insp1_RM_Z_UsedCount = 0;
                        ParameterHelper.MonitorDatas.SavePara();
                    }
                    break;
                case "Insp2TimesClear":
                    {
                        ParameterHelper.SysParam.LifeTime.Clamp.Insp2_RM_Z_UsedCount = 0;
                        ParameterHelper.MonitorDatas.SavePara();
                    }
                    break;
                case "Insp3TimesClear":
                    {
                        ParameterHelper.SysParam.LifeTime.Clamp.Insp3_RM_Z_UsedCount = 0;
                        ParameterHelper.MonitorDatas.SavePara();
                    }
                    break;
                case "Insp4TimesClear":
                    {
                        ParameterHelper.SysParam.LifeTime.Clamp.Insp4_RM_Z_UsedCount = 0;
                        ParameterHelper.MonitorDatas.SavePara();
                    }
                    break;
                case "Insp1_Z_Rst":
                    {
                        ProcedureHelper_Exc.Insp1_RM_Z_RstForce = true;
                    }
                    break;
                case "Insp2_Z_Rst":
                    {
                        ProcedureHelper_Exc.Insp2_RM_Z_RstForce = true;
                    }
                    break;
                case "Insp3_Z_Rst":
                    {
                        ProcedureHelper_Exc.Insp3_RM_Z_RstForce = true;
                    }
                    break;
                case "Insp4_Z_Rst":
                    {
                        ProcedureHelper_Exc.Insp4_RM_Z_RstForce = true;
                    }
                    break;
            }
        }

        private void cbx_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox btn = sender as CheckBox;
            string[] info = btn.Tag.ToString().Split('|');
            switch (info[1])
            {
               
                case "signalTest":
                    {
                        if (btn.Checked)
                        {
                            btn_ToUp_CH1_Ready.Enabled = true;
                            btn_ToUp_CH2_Ready.Enabled = true;
                            btn_ToUp_CH3_Ready.Enabled = true;
                            btn_ToUp_CH4_Ready.Enabled = true;
                            btn_ToUp_CH1_Receiving.Enabled = true;
                            btn_ToUp_CH2_Receiving.Enabled = true;
                            btn_ToUp_CH3_Receiving.Enabled = true;
                            btn_ToUp_CH4_Receiving.Enabled = true;

                            btn_ToDn_CH1_Rdy.Enabled = true;
                            btn_ToDn_CH2_Rdy.Enabled = true;
                            btn_ToDn_CH3_Rdy.Enabled = true;
                            btn_ToDn_CH4_Rdy.Enabled = true;
                            btn_ToDn_CH1_Sending.Enabled = true;
                            btn_ToDn_CH2_Sending.Enabled = true;
                            btn_ToDn_CH3_Sending.Enabled = true;
                            btn_ToDn_CH4_Sending.Enabled = true;
                        }
                        else
                        {
                            btn_ToUp_CH1_Ready.Enabled = false;
                            btn_ToUp_CH2_Ready.Enabled = false;
                            btn_ToUp_CH3_Ready.Enabled = false;
                            btn_ToUp_CH4_Ready.Enabled = false;
                            btn_ToUp_CH1_Receiving.Enabled = false;
                            btn_ToUp_CH2_Receiving.Enabled = false;
                            btn_ToUp_CH3_Receiving.Enabled = false;
                            btn_ToUp_CH4_Receiving.Enabled = false;

                            btn_ToDn_CH1_Rdy.Enabled = false;
                            btn_ToDn_CH2_Rdy.Enabled = false;
                            btn_ToDn_CH3_Rdy.Enabled = false;
                            btn_ToDn_CH4_Rdy.Enabled = false;
                            btn_ToDn_CH1_Sending.Enabled = false;
                            btn_ToDn_CH2_Sending.Enabled = false;
                            btn_ToDn_CH3_Sending.Enabled = false;
                            btn_ToDn_CH4_Sending.Enabled = false;
                        }
                    }
                    break;
                case "Single":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_EQ.SingleAuto = true;
                            btnNext.Visible = true;
                            btnInsp1_Next.Visible = true;
                            btnInsp2_Next.Visible = true;
                            btnInsp3_Next.Visible = true;
                            btnInsp4_Next.Visible = true;
                        }
                        else
                        {
                            ProcedureHelper_EQ.SingleAuto = false;
                            ProcedureHelper_Exc.Insp1_ToNext = false;
                            ProcedureHelper_Exc.Insp2_ToNext = false;
                            ProcedureHelper_Exc.Insp3_ToNext = false;
                            ProcedureHelper_Exc.Insp4_ToNext = false;
                            ProcedureHelper_Exc.AutoNext = false;
                            btnNext.Visible = false;
                            btnInsp1_Next.Visible = false;
                            btnInsp2_Next.Visible = false;
                            btnInsp3_Next.Visible = false;
                            btnInsp4_Next.Visible = false;
                        }
                    }
                    break;
                case "ScanLoop":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_EQ.SortLoop = true;
                        }
                        else
                        {
                            ProcedureHelper_EQ.SortLoop = false;
                        }
                    }
                    break;                
                case "CodeReader1Pass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.CodeRead1_Pass = true;
                          
                        }
                        else
                        {
                            ProcedureHelper_Exc.CodeRead1_Pass = false;
                        }
                    }
                    break;
                case "CodeReader2Pass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.CodeRead2_Pass = true;
                        }
                        else
                        {
                            ProcedureHelper_Exc.CodeRead2_Pass = false;
                        }
                    }
                    break;
                case "ExcPreAlignPass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.ExcSTPreAlignPass = true;
                        }
                        else
                        {
                            ProcedureHelper_Exc.ExcSTPreAlignPass = false;
                        }
                    }
                    break;
                case "Insp1Pass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.Insp1_Pass= true;
                        }
                        else
                        {
                            ProcedureHelper_Exc.Insp1_Pass = false;
                        }
                    }
                    break;
                case "Insp2Pass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.Insp2_Pass = true;
                        }
                        else
                        {
                            ProcedureHelper_Exc.Insp2_Pass = false;
                        }
                    }
                    break;
                case "Insp3Pass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.Insp3_Pass = true;
                        }
                        else
                        {
                            ProcedureHelper_Exc.Insp3_Pass = false;
                        }
                    }
                    break;
                case "Insp4Pass":
                    {
                        if (btn.Checked)
                        {
                            ProcedureHelper_Exc.Insp4_Pass = true;
                        }
                        else
                        {
                            ProcedureHelper_Exc.Insp4_Pass = false;
                        }
                    }
                    break;

            }
        }

        private void FreshStatus()
        {
            lbl_Insp2_RM_Z_Value.Text = Kernel.motion.Rm_Insp2_Z.Torque.ToString("F2");
            lbl_Insp3_RM_Z_Value.Text = Kernel.motion.Rm_Insp3_Z.Torque.ToString("F2");
            lbl_Insp1_RM_Z_Value.Text = Kernel.motion.Rm_Insp1_Z.Torque.ToString("F2");
            lbl_Insp4_RM_Z_Value.Text = Kernel.motion.Rm_Insp4_Z.Torque.ToString("F2");
            btnNext.Monitor(ProcedureHelper_Exc.AutoNext);
            btnInsp1_Next.Monitor(ProcedureHelper_Exc.LDTR1_ToNext);
            btnInsp2_Next.Monitor(ProcedureHelper_Exc.LDTR2_ToNext);
            btnInsp3_Next.Monitor(ProcedureHelper_Exc.ULDTR1_ToNext);
            btnInsp4_Next.Monitor(ProcedureHelper_Exc.ULDTR2_ToNext);

            btn_FrmUp_CH1_Rdy.Monitor(Kernel.Exc.DI.X060_UpStr1_SendReady==1);
            btn_FrmUp_CH1_Sending.Monitor(Kernel.Exc.DI.X061_UpStr1_Sending == 1);
            btn_FrmUp_CH2_Rdy.Monitor(Kernel.Exc.DI.X062_UpStr2_SendReady == 1);
            btn_FrmUp_CH2_Sending.Monitor(Kernel.Exc.DI.X063_UpStr2_Sending == 1);
            btn_FrmUp_CH3_Rdy.Monitor(Kernel.Exc.DI.X064_UpStr3_SendReady == 1);
            btn_FrmUp_CH3_Sending.Monitor(Kernel.Exc.DI.X065_UpStr3_Sending == 1);
            btn_FrmUp_CH4_Rdy.Monitor(Kernel.Exc.DI.X066_UpStr4_SendReady == 1);
            btn_FrmUp_CH4_Sending.Monitor(Kernel.Exc.DI.X067_UpStr4_Sending == 1);
            btn_FrmDn_CH1_Rdy.Monitor(Kernel.Exc.DI.X068_DnStr1_RevReady == 1);
            btn_FrmDn_CH1_Receiving.Monitor(Kernel.Exc.DI.X069_DnStr1_Receiving == 1);
            btn_FrmDn_CH2_Rdy.Monitor(Kernel.Exc.DI.X06A_DnStr2_RevReady == 1);
            btn_FrmDn_CH2_Receiving.Monitor(Kernel.Exc.DI.X06B_DnStr2_Receiving == 1);
            btn_FrmDn_CH3_Rdy.Monitor(Kernel.Exc.DI.X06C_DnStr3_RevReady == 1);
            btn_FrmDn_CH3_Receiving.Monitor(Kernel.Exc.DI.X06D_DnStr3_Receiving == 1);
            btn_FrmDn_CH4_Rdy.Monitor(Kernel.Exc.DI.X06E_DnStr4_RevReady == 1);
            btn_FrmDn_CH4_Receiving.Monitor(Kernel.Exc.DI.X06F_DnStr4_Receiving == 1);


            btn_ToUp_CH1_Ready.Monitor(Kernel.Exc.DO.Y060_UpStr_CH1_Rev_Ready == 1);
            btn_ToUp_CH1_Receiving.Monitor(Kernel.Exc.DO.Y061_UpStr_CH1_receiving == 1);
            btn_ToUp_CH2_Ready.Monitor(Kernel.Exc.DO.Y062_UpStr_CH2_Rev_Ready == 1);
            btn_ToUp_CH2_Receiving.Monitor(Kernel.Exc.DO.Y063_UpStr_CH2_receiving == 1);
            btn_ToUp_CH3_Ready.Monitor(Kernel.Exc.DO.Y064_UpStr_CH3_Rev_Ready == 1);
            btn_ToUp_CH3_Receiving.Monitor(Kernel.Exc.DO.Y065_UpStr_CH3_receiving == 1);
            btn_ToUp_CH4_Ready.Monitor(Kernel.Exc.DO.Y066_UpStr_CH4_Rev_Ready == 1);
            btn_ToUp_CH4_Receiving.Monitor(Kernel.Exc.DO.Y067_UpStr_CH4_receiving == 1);
            btn_ToDn_CH1_Rdy.Monitor(Kernel.Exc.DO.Y068_DnStr_CH1_SendReady == 1);
            btn_ToDn_CH1_Sending.Monitor(Kernel.Exc.DO.Y069_DnStr_CH1_Sending == 1);
            btn_ToDn_CH2_Rdy.Monitor(Kernel.Exc.DO.Y06A_DnStr_CH2_SendReady == 1);
            btn_ToDn_CH2_Sending.Monitor(Kernel.Exc.DO.Y06B_DnStr_CH2_Sending == 1);
            btn_ToDn_CH3_Rdy.Monitor(Kernel.Exc.DO.Y06C_DnStr_CH3_SendReady == 1);
            btn_ToDn_CH3_Sending.Monitor(Kernel.Exc.DO.Y06D_DnStr_CH3_Sending == 1);
            btn_ToDn_CH4_Rdy.Monitor(Kernel.Exc.DO.Y06E_DnStr_CH4_SendReady == 1);
            btn_ToDn_CH4_Sending.Monitor(Kernel.Exc.DO.Y06F_DnStr_CH4_Sending == 1);

            btnReadCode1Status.Monitor(Kernel.codeRead1.Connected);
            btnReadCode2Status.Monitor(Kernel.codeRead2.Connected);
        }

        String UldStartTrayNoCopy = "";
        private void tBoxUldStartTrayNo_Enter(object sender, EventArgs e)
        {
            //UldStartTrayNoCopy = tBoxUldStartTrayNo.Text;
        }

        float UldStartTrayNo = 0;
        private void tBoxUldStartTrayNo_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyValue == 13)
            //{
            //    if (float.TryParse(tBoxUldStartTrayNo.Text, out UldStartTrayNo))
            //    {
            //        if (UldStartTrayNo < 1)
            //        {
            //            tBoxUldStartTrayNo.Text = "1";
            //        }
            //        if (UldStartTrayNo > ParameterHelper.CurParam.Panel.Info.Num)
            //        {
            //            tBoxUldStartTrayNo.Text = Convert.ToString(ParameterHelper.CurParam.Panel.Info.Num);
            //        }
            //        ProcedureHelper_Exc.TrayRoadSetStartIdx = Convert.ToInt32(float.Parse(tBoxUldStartTrayNo.Text))-1;

            //        UldStartTrayNoCopy = tBoxUldStartTrayNo.Text;
            //    }
            //    else
            //    {
            //        tBoxUldStartTrayNo.Text = UldStartTrayNoCopy;
            //    }
            //    this.ActiveControl=label2;
            //}
        }
        private void tBoxUldStartTrayNo_Leave(object sender, EventArgs e)
        {
            
        }

        private void btnInsp2TimesClear_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void pcButton1_Click(object sender, EventArgs e)
        {

        }
    }
}
